<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>修改文件</title>
    <link rel="stylesheet" href="../layui-v2.6.8/layui/css/layui.css">
    <script src="../layui-v2.6.8/layui/layui.js"></script>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <link rel="stylesheet" href="css/index.css">
</head>
<body>
<div class="layui-container" style="margin-top: 20px">
    <input type="text" name="title" required id="title"  lay-verify="required" placeholder="文档标题" autocomplete="off" class="layui-input">
    <br>
    <textarea id="txt" required lay-verify="required" placeholder="请输入" class="layui-textarea"></textarea>
    <div style="text-align: right;margin-top: 20px">
        <button class="layui-btn" onclick="save()">保存</button>
        <button class="layui-btn layui-btn-primary" onclick="close_this()">取消</button>
    </div>
</div>
</body>
<script>
    console.log("当前文件名：" + vm.file_name);
    function close_this(){
        layer.close(vm.index);
    }
    $.get("../api/read_file.php",{
        "f":vm.file_list[vm.file_index]['file_name']
    },function (data){
        $("#txt").val(data);
        $("#title").val(vm.file_list[vm.file_index]['file_title'])
    })
    //文本框的高度随着文字的增加而变化
    $('#txt').each(function () {
        this.setAttribute('style', 'height:' + (this.scrollHeight) + 'px;overflow-y:hidden;');
    }).on('input', function () {
        this.style.height = 'auto';
        this.style.height = (this.scrollHeight) + 'px';
    });

    function save(){
        $.post("../api/write_file.php",{
            "id":getCookie("user_id"),
            "Token":getCookie("Token"),
            "userTime":Date.parse(new Date()),
            "file_name":vm.file_list[vm.file_index]['file_name'],
            "txt":$("#txt").val(),
            "title":$("#title").val()
        },function (data) {
            let data_json = JSON.parse(data);
            if(data_json['status'] == 200){
                vm.get_file_list();
                layer.msg("修改成功");
                close_this();
            }
        })
    }
</script>
</html>